package com.xilihui.xlh.business.activitys.store;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.AddressEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Address;

/**
 * Created by Administrator on 2018/9/4.
 */

public class AddressActivity extends ToolBaseCompatActivity implements OnRefreshListener, OnLoadMoreListener, View.OnClickListener {

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    ExceptionManager exceptionManager;
    BaseAdapter<AddressEntity.AddressListBean> baseAdapter;
    ArrayList<AddressEntity.AddressListBean> data = new ArrayList<>();
    
    Activity activity = this;
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void addAddress(){
        Intent intent = new Intent(this,AddAddressActivity.class);
        startActivity(intent);
    }
    
    @Override
    public int getLayout() {
        return R.layout.activity_address;
    }
    
    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        setToolTitle("收货地址管理");
        setToolRightText("添加新地址");
        
        exceptionManager = new ExceptionManager(smartRefreshLayout,new DefaultExceptionListener(this)){

            @Override
            protected int getEmptyView() {
                return R.layout.empty_address;
            }
        };
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        smartRefreshLayout.setEnableLoadMore(false);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.view_line,
                R.dimen.divider_space, LinearLayout.VERTICAL));
        
        baseAdapter = new BaseAdapter<AddressEntity.AddressListBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_address;
            }

            @Override
            protected void getView(BaseViewHolder holder, final AddressEntity.AddressListBean entity, final int position) {
                holder.setText(R.id.tv_name,entity.getConsignee());
                holder.setText(R.id.tv_phone,entity.getMobile());
                holder.setText(R.id.tv_address,entity.getProvince().getName()+
                        entity.getCity().getName()+entity.getDistrict().getName()+entity.getAddress());
                
                TextView textView = (TextView)holder.getView(R.id.tv_check);
                if(entity.getIs_default().equals("1")){
                    textView.setSelected(true);
                }else {
                    textView.setSelected(false);
                }
                
                holder.setOnClickListener(R.id.tv_check, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        StoreRequest.updateAddress(AddressActivity.this,entity.getAddress_id(),
                                entity.getConsignee(),entity.getProvince().getId(),entity.getCity().getId(),
                                entity.getDistrict().getId(),entity.getMobile(),"1",entity.getAddress())
                                .compose(DoTransform.<BaseEntity>applyScheduler((FragmentActivity)activity,true))
                                .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                                    @Override
                                    protected void onSuccess(BaseEntity entity) {
                                        getData(false);
                                    }

                                    @Override
                                    protected void onFail(String msg) {
                                        ToastUtil.toastShortNegative(msg);
                                    }
                                });
                    }
                });
                
                holder.setOnClickListener(R.id.tv_edit, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(AddressActivity.this,AddAddressActivity.class);
                        intent.putExtra("data",data.get(position));
                        startActivity(intent);
                    }
                });
                
                
                holder.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        StoreRequest.deleteAddress(activity,data.get(position).getAddress_id())
                                .compose(DoTransform.<BaseEntity>applyScheduler((FragmentActivity) activity,true))
                                .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                                    @Override
                                    protected void onSuccess(BaseEntity entity) {
                                        ToastUtil.toastShortPositive(entity.getMsg());
                                        getData(true);
                                    }

                                    @Override
                                    protected void onFail(String msg) {
                                        ToastUtil.toastShortNegative(msg);
                                    }
                                });
                    }
                });
                
            }
        };
        
        
        recyclerView.setAdapter(baseAdapter);
        baseAdapter.setAnimationsLocked(true);
        
    }

    @Override
    public void initData() {
        getData(true);
    }
    
    public void getData(boolean isLoading){
        StoreRequest.address(AddressActivity.this)
                .compose(DoTransform.<AddressEntity>applyScheduler(this,isLoading))
                .compose(this.<AddressEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<AddressEntity>(this) {
                    @Override
                    protected void onSuccess(AddressEntity entity) {
                        data.clear();
                        data.addAll(entity.getAddress_list());
                        
                        if(data.size()>0){
                            exceptionManager.hide();
                        }else {
                            exceptionManager.showEmpty();
                        }
                        
                        baseAdapter.setList(data);
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
                
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        
    }

    @Override
    public void onClick(View view) {
        
    }

    @Override
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is("add_address")){
            getData(false);
        }
    }
}
